﻿
using AutoMachine;
using CommonTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Tool;

namespace Motions
{
    class ZDMotion : Motions
    {
        private IntPtr g_handle;//链接返回的句柄，可以作为卡号
      //  private string m_strIP;

        public ZDMotion(int nCardIndex, string strName, int nMinAxisNo, int nMaxAxisNo)
            : base(nCardIndex, strName, nMinAxisNo, nMaxAxisNo)
        {
            m_bEnable = false;
        }

      
        /// <summary>
        /// 板卡初始化
        /// </summary>
        /// <returns></returns>
        public override void InitMotionCard()
        {
            int rtn = 0;
            //链接控制器 
             rtn += zmcaux.ZAux_OpenEth("192.168.0.11", out g_handle);//打开网口控制器       
            StringBuilder cmdbuffack = new StringBuilder(2048);
            //  rtn += zmcaux.ZAux_Execute(g_handle,"Ecat_Init", cmdbuffack,2048);
            //rtn += zmcaux.ZAux_BasDown(g_handle, AppDomain.CurrentDomain.BaseDirectory + "temp.bas",0);
            //单个.bas 文件生成 ZAR 并且下载到控制器运行  连接句柄 路径 下载模式 0-RAM 1-ROM。
            float a = 0;
            rtn += zmcaux.ZAux_Direct_GetUserVar(g_handle, "Bus_InitStatus", ref a);
            if (a == 1)
            {
                LogView.GetInstance().ShowLog(LogView.LogType.Oper, "正运动初始化成功");
                m_bEnable = true;//初始化成功
                return;
            }

          
            //if (rtn == 0)
            //{
            //    m_bEnable = true;//初始化成功
            //    return;
            //}
            //if (g_handle != (IntPtr)0)
            //{
            //    m_bEnable = true;//初始化成功
            //    return;
            //}
            m_bEnable = false;
            //MessageBox.Show(string.Format("正运控控制卡,卡号: {1}, result = {0}"));
            LogView.GetInstance().ShowLog(LogView.LogType.Error,  "正运动初始化失败");
            return;

        }

        /// <summary>
        /// 关闭轴卡
        /// </summary>
        /// <returns></returns>
        public override void DeInitMotionCard()
        {
          //  IntPtr g_handle = (IntPtr)0;
            //连接句柄
            zmcaux.ZAux_Close(g_handle);
            g_handle = (IntPtr)0;
        }

        /// <summary>
        /// 开启使能
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void ServoOn(int nAxisNo)
        {
            int rtn = 0;
            //连接句柄 轴号 0 关闭 1打开
            rtn += zmcaux.ZAux_Direct_SetAxisEnable(g_handle, nAxisNo, 1);
            if (rtn != 0)
            {
                MessageBox.Show(string.Format("第{0}张卡第{1}个轴开始使能失败原因{2} ---- ", m_nCardIndex, nAxisNo, rtn));
                LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("第{0}张卡第{1}个轴开始使能失败原因{2} ---- ", m_nCardIndex, nAxisNo, rtn));
            }
        }

        /// <summary>
        /// 关闭使能
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void ServoOff(int nAxisNo)
        {
            int rtn = 0;
            //连接句柄 轴号 0 关闭 1打开
            rtn += zmcaux.ZAux_Direct_SetAxisEnable(g_handle, nAxisNo, 0);
            if (rtn != 0)
            {
                MessageBox.Show(string.Format("第{0}张卡第{1}个轴开始使能失败原因{2} ---- ", m_nCardIndex, nAxisNo, rtn));
                LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("第{0}张卡第{1}个轴开始使能失败原因{2} ---- ", m_nCardIndex, nAxisNo, rtn));
            }

        }

        /// <summary>
        /// 清除轴报警信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>                   
        public override void ClearAxisAlarm(int nAxisNo)
        {
            int rtn = 0;
            //设置清除总线伺服报警
            //连接句柄  轴号 0-清除当前 1-清除历史 2-清除外部输入警告

            rtn = zmcaux.ZAux_BusCmd_DriveClear(g_handle, (uint)nAxisNo, 0);
        }

        /// <summary>
        /// 读取指定轴Busy状态
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisBusy(int nAxisNo)
        {
            int rtn = 0;
            int nsts = 0;
            // 读取当前轴运动是否在运动中 0运动中 -1未运动
            rtn = zmcaux.ZAux_Direct_GetIfIdle(g_handle, nAxisNo, ref nsts);
            if (rtn == 0)
            {
                return nsts == 0;
            }
            return false;
        }

        /// <summary>
        /// 读取指定轴报警信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisAlarm(int nAxisNo)
        {
            //1 随动误差超限告警            0x2
            //2 与远程轴通讯出错            0x4
            //3 远程驱动器报错            0x8
            //4 正向硬限位            0x10
            //5 反向硬限位                      0x20
            //6 找原点中            0x40
            //7 HOLD 速度保持信号输入            0x80
            //8 随动误差超限出错            0x100
            //9 超过正向软限位         0x200
            //10 超过负向软限位       0x400
            //11 CANCEL 执行中    0x800
            //12 脉冲频率超过 MAX_SPEED 限制需要修改降速或修改    MAX_SPEED
            //13机械手指令坐标错误 16384 4000h
            //18 电源异常
            //21 运动中触发特殊运动指令失败
            //22 告警信号输入 
            //23 轴进入了暂停状态

            int rtn = 0;
            int nsts = 0;
            rtn = zmcaux.ZAux_Direct_GetAxisStatus(g_handle, nAxisNo, ref nsts);
            if (rtn == 0)
            {
                if (((nsts >> 22) & 1) == 1)
                {
                    return true;
                }
            }
            return false;


        }

        /// <summary>
        /// 读取指定轴伺服准备信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisReady(int nAxisNo)
        {
            //1 随动误差超限告警            0x2
            //2 与远程轴通讯出错            0x4
            //3 远程驱动器报错            0x8
            //4 正向硬限位            0x10
            //5 反向硬限位                      0x20
            //6 找原点中            0x40
            //7 HOLD 速度保持信号输入            0x80
            //8 随动误差超限出错            0x100
            //9 超过正向软限位         0x200
            //10 超过负向软限位       0x400
            //11 CANCEL 执行中    0x800
            //12 脉冲频率超过 MAX_SPEED 限制需要修改降速或修改    MAX_SPEED
            //13机械手指令坐标错误 16384 4000h
            //18 电源异常
            //21 运动中触发特殊运动指令失败
            //22 告警信号输入 
            //23 轴进入了暂停状态

            int rtn = 0;
            int nsts = 0;
            rtn = zmcaux.ZAux_Direct_GetAxisStatus(g_handle, nAxisNo, ref nsts);
            if (rtn == 0)
            {
                //没有返回值0 代表轴正常
              return  nsts == 0;
            }
            return false;
        }

        /// <summary>
        /// 读取轴使能状态
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisSeverOn(int nAxisNo)
        {
            int rtn = 0;
            int nsts = 0;
            //读取轴使能状态
            //连接标识 轴号 返回使能状态 成功返回值为0  0表示关闭 1表示打开
            rtn = zmcaux.ZAux_Direct_GetAxisEnable(g_handle, nAxisNo, ref nsts);
            if (rtn == 0)
            {
                return nsts == 1;

            }
            return false;
        }

        /// <summary>
        /// 获取指定轴当前位置
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override double GetAxisActPos(int nAxisNo)
        {
            int rtn = 0;
            float pos = 0;
             //读取轴测量反馈位置
             //连接标识 轴号 位置
            rtn = zmcaux.ZAux_Direct_GetMpos(g_handle, nAxisNo, ref pos);
            if (0 == rtn)
            {
                return (double)pos/* / SystemCfg.AxisCfg[nAxisNo].PluseRatio */;
            }
            return 0;
        }

        /// <summary>
        /// 获取指定轴命令位置
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override double GetAxisPrfPos(int nAxisNo)
        {
            int rtn = 0;
            float pos = 0;
            //读取轴的规划位置
            rtn = zmcaux.ZAux_Direct_GetDpos(g_handle, nAxisNo, ref pos);
            if (0 == rtn)
            {
                return (double)pos /*/SystemCfg.AxisCfg[nAxisNo].PluseRatio*/ ;
            }
            return 0;
        }

        /// <summary>
        /// 获取轴定位完成信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisDone(int nAxisNo)
        {
            //无正限位 负限位 Busy信号无  有使能 伺服无报警 

            //1 随动误差超限告警            0x2
            //2 与远程轴通讯出错            0x4
            //3 远程驱动器报错            0x8
            //4 正向硬限位            0x10
            //5 反向硬限位                      0x20
            //6 找原点中            0x40
            //7 HOLD 速度保持信号输入            0x80
            //8 随动误差超限出错            0x100
            //9 超过正向软限位         0x200
            //10 超过负向软限位       0x400
            //11 CANCEL 执行中    0x800
            //12 脉冲频率超过 MAX_SPEED 限制需要修改降速或修改    MAX_SPEED
            //13机械手指令坐标错误 16384 4000h
            //18 电源异常
            //21 运动中触发特殊运动指令失败
            //22 告警信号输入 
            //23 轴进入了暂停状态
        
            int rtn = 0;
            int nsts = 0;
            int nsts1 = 0;
            int nsts2 = 0;
            rtn += zmcaux.ZAux_Direct_GetAxisEnable(g_handle, nAxisNo, ref nsts);
            rtn += zmcaux.ZAux_Direct_GetAxisStatus(g_handle, nAxisNo, ref nsts1);
            rtn += zmcaux.ZAux_Direct_GetIfIdle(g_handle, nAxisNo, ref nsts2);
            if (rtn == 0)
            {
                if (nsts == 1)//有使能
                {
                    if (((nsts1 >> 4) & 1) == 0)//无正限位
                    {
                        if (((nsts1 >> 5) & 1) == 0)//无负限位
                        {
                            if (((nsts1 >> 22) & 1) == 0)//无报警
                            {
                                if (nsts2 != 0)//不Busy
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
                
            }
            return false;
        }

        /// <summary>
        /// 读取指定轴原点信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisHome(int nAxisNo)
        {
            return false;
        }

        /// <summary>
        /// 读取指定轴负极限
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetNegLimit(int nAxisNo)
        {

            //1 随动误差超限告警            0x2
            //2 与远程轴通讯出错            0x4
            //3 远程驱动器报错            0x8
            //4 正向硬限位            0x10
            //5 反向硬限位                      0x20
            //6 找原点中            0x40
            //7 HOLD 速度保持信号输入            0x80
            //8 随动误差超限出错            0x100
            //9 超过正向软限位         0x200
            //10 超过负向软限位       0x400
            //11 CANCEL 执行中    0x800
            //12 脉冲频率超过 MAX_SPEED 限制需要修改降速或修改    MAX_SPEED
            //13机械手指令坐标错误 16384 4000h
            //18 电源异常
            //21 运动中触发特殊运动指令失败
            //22 告警信号输入 
            //23 轴进入了暂停状态

            int rtn = 0;
            int nsts = 0;
            rtn = zmcaux.ZAux_Direct_GetAxisStatus(g_handle, nAxisNo, ref nsts);
            if (rtn == 0)
            {
                if (((nsts >> 5) & 1) == 1)
                {
                    return true;
                }
            }
            return false;

        }

        /// <summary>
        /// 读取指定轴正极限
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetPosLimit(int nAxisNo)
        {
            //1 随动误差超限告警            0x2
            //2 与远程轴通讯出错            0x4
            //3 远程驱动器报错            0x8
            //4 正向硬限位            0x10
            //5 反向硬限位                      0x20
            //6 找原点中            0x40
            //7 HOLD 速度保持信号输入            0x80
            //8 随动误差超限出错            0x100
            //9 超过正向软限位         0x200
            //10 超过负向软限位       0x400
            //11 CANCEL 执行中    0x800
            //12 脉冲频率超过 MAX_SPEED 限制需要修改降速或修改    MAX_SPEED
            //13机械手指令坐标错误 16384 4000h
            //18 电源异常
            //21 运动中触发特殊运动指令失败
            //22 告警信号输入 
            //23 轴进入了暂停状态

            int rtn = 0;
            int nsts = 0;
            rtn = zmcaux.ZAux_Direct_GetAxisStatus(g_handle, nAxisNo, ref nsts);
            if (rtn == 0)
            {
                if (((nsts >> 4) & 1) == 1)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 指定轴减速停止
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void StopAxis(int nAxisNo)
        {
            int rtn = 0;
            //单轴运动停止
            //连接标识 轴号 模式 0取消当前运动 1取消缓冲中的运动 2 取消当前运动和缓冲运动 3 立即中断脉冲发送
            rtn = zmcaux.ZAux_Direct_Single_Cancel(g_handle, nAxisNo, 2);
            if (rtn != 0)
            {

            }
        }

        /// <summary>
        /// 指定轴紧急停止
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void EmgAxis(int nAxisNo)
        {
            int rtn = 0;
            //单轴运动停止
            //连接标识 轴号 模式 0取消当前运动 1取消缓冲中的运动 2 取消当前运动和缓冲运动 3 立即中断脉冲发送
            rtn = zmcaux.ZAux_Direct_Single_Cancel(g_handle, nAxisNo, 3);
            if (rtn != 0)
            {

            }
        }

        /// <summary>
        /// 指定轴位置清零
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void SetPosZero(int nAxisNo)
        {
            int rtn = 0;
            //设置轴的规划位置
            rtn += zmcaux.ZAux_Direct_SetDpos(g_handle, nAxisNo, 0);
            rtn += zmcaux.ZAux_Direct_SetMpos(g_handle, nAxisNo, 0);
            if (rtn != 0)
            {

            }
        }

        /// <summary>
        /// 读取输入信号
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public override bool ReadIn(int nCardN0, int nIndex)
        {
            int rtn = 0;
            uint Data = 0;         
            //连接句柄 输入口编号
            rtn = zmcaux.ZAux_Direct_GetIn(g_handle, nIndex, ref Data);
            if (rtn == 0)
            {
                return 1 == Data;
            }   
            return false;
        }

        /// <summary>
        /// 读取输出信号
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public override bool ReadOut(int nCardN0, int nIndex)
        {
            int rtn = 0;
            uint Data = 0;         
            //连接句柄 输入口编号 状态
            rtn = zmcaux.ZAux_Direct_GetOp(g_handle, nIndex, ref Data );
            if (rtn == 0)
            {
                return 1 == Data;
            }
            return false;
        }

        /// <summary>
        /// 输出信号
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="bBit"></param>
        /// <returns></returns>
        public override void WriteOut(int nCardN0, int nIndex, bool bBit)
        {
            int rtn = 0;
            uint Postion = 2;
            if (bBit == true)
            {
                Postion = 1;
            }
            else
            {
                Postion = 0;
            }
            //连接句柄 输出口 0 关闭 1 打开
            rtn =  zmcaux.ZAux_Direct_SetOp(g_handle, nIndex, Postion);
        }

        /// <summary>
        /// 指定轴绝对位置移动
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <param name="nPos"></param>
        /// <param name="nSpeed"></param>
        /// <returns></returns>
        public override void AbsMove(int nAxisNo, double Pos, double Speed)
        {
            int rtn = 0;



            float nSpeed = (float)(Convert.ToSingle(Speed) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float nPos = Convert.ToSingle(Pos) /** SystemCfg.AxisCfg[nAxisNo].PluseRatio*/;
            float Acc = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].Acc) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float Dec = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].Dec) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float StartSpeed = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].StartSpeed) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float Sramp  = Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].SmoothTime);
            //设置轴参数
            //设置轴类型 连接标识 轴号 类型 0虚拟轴 1 1 脉冲方向方式的步进或伺服 2 模拟信号控制方式的伺服
            //3 正交编码器 4脉冲方向输出+正交编码器输入 5脉冲方向输出+脉冲方向编码器输入
            //8ZCAN 扩展脉冲方向方式步进或伺服 9 ZCAN 扩展正交编码器 10 ZCAN 扩展脉冲方向方式的编码器 
           // rtn += zmcaux.ZAux_Direct_SetAtype(g_handle, nAxisNo, 1);
            //设置脉冲当量
         //    rtn += zmcaux.ZAux_Direct_SetUnits(g_handle, nAxisNo, 1);
            //设置轴起始速度
            rtn += zmcaux.ZAux_Direct_SetLspeed(g_handle, nAxisNo, StartSpeed);
            //设置轴速度
            rtn += zmcaux.ZAux_Direct_SetSpeed(g_handle, nAxisNo, nSpeed);
            //设置轴加速度
            rtn += zmcaux.ZAux_Direct_SetAccel(g_handle, nAxisNo, nSpeed * 10);
            //设置轴减速度
            rtn += zmcaux.ZAux_Direct_SetDecel(g_handle, nAxisNo, nSpeed * 10);
            //设置轴S曲线设置 200ms
            rtn += zmcaux.ZAux_Direct_SetSramp(g_handle, nAxisNo, 200);

            //单轴相对运动  轴号 距离 
            rtn += zmcaux.ZAux_Direct_Single_MoveAbs(g_handle, nAxisNo, nPos);
            if (rtn != 0)
            {
                return;
            }
        }

        /// <summary>
        /// 指定轴相对运动
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <param name="nPos"></param>
        /// <param name="nSpeed"></param>
        /// <returns></returns>
        public override void RelMove(int nAxisNo, double Pos, double Speed)
        {

            int rtn = 0;
            float nSpeed = (float)(Convert.ToSingle(Speed) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float nPos = Convert.ToSingle(Pos) /** SystemCfg.AxisCfg[nAxisNo].PluseRatio*/;
            float Acc = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].Acc) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float Dec = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].Dec) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float StartSpeed = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].StartSpeed) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float Sramp = Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].SmoothTime);
            //设置轴参数
            //设置轴类型 连接标识 轴号 类型 0虚拟轴 1 1 脉冲方向方式的步进或伺服 2 模拟信号控制方式的伺服
            //3 正交编码器 4脉冲方向输出+正交编码器输入 5脉冲方向输出+脉冲方向编码器输入
            //8ZCAN 扩展脉冲方向方式步进或伺服 9 ZCAN 扩展正交编码器 10 ZCAN 扩展脉冲方向方式的编码器 
        //    rtn += zmcaux.ZAux_Direct_SetAtype(g_handle, nAxisNo, 1);
            //设置轴脉冲当量
         //   rtn += zmcaux.ZAux_Direct_SetUnits(g_handle, nAxisNo, 1);
            //设置轴起始速度
            rtn += zmcaux.ZAux_Direct_SetLspeed(g_handle, nAxisNo, StartSpeed);
            //设置轴速度
            rtn += zmcaux.ZAux_Direct_SetSpeed(g_handle, nAxisNo, nSpeed);
            //设置轴加速度
            rtn += zmcaux.ZAux_Direct_SetAccel(g_handle, nAxisNo, Acc);
            //设置轴减速度
            rtn += zmcaux.ZAux_Direct_SetDecel(g_handle, nAxisNo, Dec);
            //设置轴S曲线设置 200ms
            rtn += zmcaux.ZAux_Direct_SetSramp(g_handle, nAxisNo, 200);

            //单轴相对运动  轴号 距离 
            rtn += zmcaux.ZAux_Direct_Single_Move(g_handle, nAxisNo, nPos);
            if (rtn != 0)
            {
                return;
            }
        }

        /// <summary>
        /// 指定轴Jog运动
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <param name="nSpeed"></param>
        /// <param name="bPositive"></param>
        /// <returns></returns>
        public override void JogMove(int nAxisNo, double Speed, bool MovDir)
        {
            int rtn = 0;
            float nSpeed = (float)(Convert.ToSingle(Speed) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float Acc = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].Acc) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float Dec = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].Dec) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float StartSpeed = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].StartSpeed) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float Sramp = Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].SmoothTime);
            int Postion = 0;
            if (MovDir == true)
            {
                Postion = 1;
            }
            else
            {
                Postion = -1;
            }
            //设置轴参数
            //设置轴类型 连接标识 轴号 类型 0虚拟轴 1 1 脉冲方向方式的步进或伺服 2 模拟信号控制方式的伺服
            //3 正交编码器 4脉冲方向输出+正交编码器输入 5脉冲方向输出+脉冲方向编码器输入
            //8ZCAN 扩展脉冲方向方式步进或伺服 9 ZCAN 扩展正交编码器 10 ZCAN 扩展脉冲方向方式的编码器 
         //   rtn +=  zmcaux.ZAux_Direct_SetAtype(g_handle, nAxisNo, 1);
            //设置轴脉冲当量
        //    rtn += zmcaux.ZAux_Direct_SetUnits(g_handle, nAxisNo, 1);
            //设置轴起始速度
              rtn += zmcaux.ZAux_Direct_SetLspeed(g_handle, nAxisNo, StartSpeed);
            //设置轴速度
            rtn += zmcaux.ZAux_Direct_SetSpeed(g_handle, nAxisNo, nSpeed);
            //设置轴加速度
            rtn += zmcaux.ZAux_Direct_SetAccel(g_handle, nAxisNo, Acc);
            //设置轴减速度
            rtn += zmcaux.ZAux_Direct_SetDecel(g_handle, nAxisNo,Dec);
            //设置轴S曲线设置 200ms
            rtn += zmcaux.ZAux_Direct_SetSramp(g_handle, nAxisNo, 200);

            //单轴连续运动 -1负向 1 正向 
            rtn+= zmcaux.ZAux_Direct_Single_Vmove(g_handle, nAxisNo, Postion);
            if (rtn != 0)
            {
                return;
            }


        }

        /// <summary>
        /// 指定轴回原点 
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <param name="MoveDir"></param>
        /// <returns></returns>
        public override void HomeMove(int nAxisNo, int MoveDir, uint HomeMode)
        {
            Task.Factory.StartNew(() =>
            {
            int rtn = 0;
            int position = (int)SystemCfg.AxisCfg[nAxisNo].HomeMode;
          
            float OrgHSpeed = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].OrgHSpeed) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            float OrgLSpeed = (float)(Convert.ToSingle(SystemCfg.AxisCfg[nAxisNo].OrgLSpeed) * SystemCfg.AxisCfg[nAxisNo].PluseRatio);
            rtn += zmcaux.ZAux_Direct_SetSpeed(g_handle, nAxisNo, OrgHSpeed);
            rtn += zmcaux.ZAux_Direct_SetCreep(g_handle, nAxisNo, OrgLSpeed);
            //13 正限位回原 14 负限位回原
            rtn += zmcaux.ZAux_Direct_Single_Datum(g_handle, nAxisNo, position);

            int status = 0;//Busy状态
            float pos = 0;//Dpos位置
            int sts = 0;//轴停止原因
            do
            {
                Thread.Sleep(1);
                //判断是否Busy   读取当前轴运动是否在运动中 0运动中 -1未运动
                rtn += zmcaux.ZAux_Direct_GetIfIdle(g_handle, nAxisNo, ref status);//先执行

                //读取轴的规划位置 规划地址为0
                rtn += zmcaux.ZAux_Direct_GetDpos(g_handle, nAxisNo, ref pos);
                //轴停止原因 正常停止0
                rtn += zmcaux.ZAux_Direct_GetAxisStopReason(g_handle, nAxisNo, ref sts);

            } while ((status == 0) && (pos != 0) && (sts == 0));//如果是True 则重复执行循环体，如果为False 则继续执行下面语句
            Thread.Sleep(100);
            rtn += zmcaux.ZAux_Direct_SetDpos(g_handle, nAxisNo, 0);
            rtn += zmcaux.ZAux_Direct_SetMpos(g_handle, nAxisNo, 0);
            });

        }

        public override void InitExtIoCard(int CarsNO)
        {
            int rtn = 0;
            //链接控制器 
            rtn += zmcaux.ZAux_BasDown(g_handle, AppDomain.CurrentDomain.BaseDirectory + "\\" + "Config" + "\\" + "ECAT初始化.bas", 1);
        }

        public override void DeInitExtIoCard(int Card)
        {
            int rtn = 0;
            StringBuilder buffer = new StringBuilder(2048);
            rtn = zmcaux.ZAux_Execute(g_handle, "Ecat_Init", buffer, 0);
        }
       

        public override bool ReadExtIn(int CardNo, int nIndex)
        {
            throw new NotImplementedException();
        }

        public override bool ReadExtOut(int CardNo, int nIndex)
        {
            throw new NotImplementedException();
        }

        public override void WriteExtOut(int CardNo, int nIndex, bool bBit)
        {
            throw new NotImplementedException();
        }

         public override bool GetBusState()
        {
            throw new NotImplementedException();
        }

        public override void ClearBusAlarm()
        {
            throw new NotImplementedException();
        }

        public override bool HomeMove1(int nAxisNo, int MoveDir)
        {
            throw new NotImplementedException();
        }

        public override void OpenCmd(int cardNum, ushort channel, uint dir)
        {
            throw new NotImplementedException();
        }

        public override void CloseCmd(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override void SetCmdData(int cardNum, ushort channel, double[] data)
        {
            throw new NotImplementedException();
        }

        public override int GetCmdFIFOCount(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override void SetCmdDO(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override void CloseCmdDO(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override void ResetCmd(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override bool HomeOK(int nAxisNo)
        {
            throw new NotImplementedException();
        }
    }
}
